﻿namespace UnitTest.Dxp.CoreText

open System
open Microsoft.VisualStudio.TestTools.UnitTesting
open Lib.Dxp.Core
open Lib.Dxp.Core.Html
open Lib.Dxp.Core.Html.Tag

[<TestClass>]
type UnitTest_DxpCoreText() = 
    
    [<TestMethod>]
    member x.CheckTxt () = 
        Assert.AreEqual("a<br/> b c<br/> d",        nl2br "a\n b\r c\n d\r")
        Assert.AreEqual("a  b c  d",                no_nl "a\n b\r c\n d\r")
        Assert.AreEqual("a  b  c  d ",              no_tabs "a\t b\t c\t d\t")
        Assert.AreEqual("a` b` c` d`",              no_quotes "a\" b' c\" d'")
        Assert.AreEqual("a &lt; 10 &amp; c &gt; 1", no_ltgtamp "a < 10 & c > 1")

        Assert.AreEqual(spp [|"a b"; " c d"; ""|],  spp <| splitBy '\'' "a b' c d'")
        Assert.AreEqual(spp [|"a"; "b"; "c"; "d"|], spp <| splitRe "\d+" "a1b12c234d")

        Assert.AreEqual("aaa\n\r\tbbb", trim "   aaa\n\r\tbbb\t  ")
        Assert.AreEqual("aaa\n\r\tbbb", trim " \r\n  aaa\n\r\tbbb\t \n\r ")

        Assert.AreEqual("abcdef",   pad_left 5 "abcdef")
        Assert.AreEqual("    a",    pad_left 5 "a")
        Assert.AreEqual("abcdef",   pad_right 5 "abcdef")
        Assert.AreEqual("a    ",    pad_right 5 "a")
        

    [<TestMethod>]
    member x.ToS () = 
        Assert.AreEqual(to_s "",    to_s (null : string))
        Assert.AreEqual(to_s "",        to_s <| "")
        Assert.AreEqual(to_s "aa",      to_s <| "aa")

        
    [<TestMethod>]
    member x.ToS_DB () = 
        Assert.AreEqual(to_s "",    to_s <| Data.SqlTypes.SqlString())
        Assert.AreEqual(to_s "",    to_s <| Data.SqlTypes.SqlString(""))
        Assert.AreEqual(to_s "aa",  to_s <| Data.SqlTypes.SqlString("aa"))

        Assert.AreEqual(to_s "Null",    string <| Data.SqlTypes.SqlString())
        Assert.AreEqual(to_s "",    string <| Data.SqlTypes.SqlString(""))
        Assert.AreEqual(to_s "aa",  string <| Data.SqlTypes.SqlString("aa"))

        Assert.AreEqual(to_s "",    to_s <| Data.SqlTypes.SqlInt32())
        Assert.AreEqual(to_s 7,    to_s <| Data.SqlTypes.SqlInt32(7))


    [<TestMethod>]
    member x.Html() = 
        Assert.AreEqual("<div />",   trim <| (div []).ToString())
        Assert.AreEqual("<span />",   trim <| (span []).ToString(id))
        Assert.AreEqual("<span><div>~aa</div> </span>",   trim <| (span [ div [localed "~aa"]]).ToString(id) |> no_nl)
        // Assert.AreEqual("1",    trim <| to_s (div [])) // "to_s" failed fo tags due Union structure
        ()

    [<TestMethod>]
    member x.Html2() = 
        Assert.AreEqual("<div />",   trim <| (div [] |> to_s))
